
use std::cell::RefCell;
use std::rc::Rc;

use neutrino::utils::pixmap::Pixmap;
use neutrino::widgets::image::{ImageListener, ImageState};
use neutrino::widgets::button::{ButtonState, ButtonListener};

use super::models::Images;

pub struct MyImageListener {
    images: Rc<RefCell<Images>>,
}

impl MyImageListener {
    pub fn new(images: Rc<RefCell<Images>>) -> Self {
        Self { images }
    }
}

impl ImageListener for MyImageListener {
    fn on_update(&self, state: &mut ImageState) {
        let pixmap = Pixmap::from_path(self.images.borrow().selected_path());
        state.set_data(pixmap.data());
        state.set_extension(pixmap.extension());
    }
}


struct TextContent {
    content: String,
}

impl TextContent {
    fn new() -> Self {
        Self { content: "...".to_string() }
    }

    fn cotent(&self) -> &str {
        &self.content
    }

    fn set_content(&mut self, name: &str) {
        self.content = name.to_string();
    }
}


struct MyTextInputListener {
    content: Rc<RefCell<TextContent>>,
}

impl MyTextInputListener {
    pub fn new(content: Rc<RefCell<TextContent>>) -> Self {
        Self { content }
    }
}

impl TextInputListener for MyTextInputListener {
    fn on_change(&self, state: &TextInputState) {
        println!("content:{} ", &state.value() ) ;
        self.content.borrow_mut().set_content(&state.value());
    }

    fn on_update(&self, state: &mut TextInputState) {
        state.set_value(&self.content.borrow().name());
    }
}

pub struct MyPrevButtonListener {
    images: Rc<RefCell<Images>>,
}

impl MyPrevButtonListener {
    pub fn new(images: Rc<RefCell<Images>>) -> Self {
        Self { images }
    }
}

impl ButtonListener for MyPrevButtonListener {
    fn on_update(&self, _state: &mut ButtonState) {}

    fn on_change(&self, _state: &ButtonState) {
        self.images.borrow_mut().previous();
    }
}

pub struct MyNextButtonListener {
    images: Rc<RefCell<Images>>,
}

impl MyNextButtonListener {
    pub fn new(images: Rc<RefCell<Images>>) -> Self {
        Self { images }
    }
}

impl ButtonListener for MyNextButtonListener {
    fn on_update(&self, _state: &mut ButtonState) {}

    fn on_change(&self, _state: &ButtonState) {
        self.images.borrow_mut().next();
    }
}